using System;

namespace Firestarter.DB.Schema.Logic
{
    public static class SqlUtilities
    {
        public static void AddSqlWhereParameter(ref string sql, string fieldName, string fieldValue)
        {
            string parameterSql = "";
            if (fieldValue != null)
            {
                parameterSql += fieldName + " = '" + fieldValue + "' ";
            }
            else
            {
                parameterSql += fieldName + " is null ";
            }
            AddSqlWhereParameter(ref sql, parameterSql);
        }

        public static void AddSqlWhereParameter(ref string sql, string parameterSql)
        {
            string sqlToAdd = "";
            if (sql.Contains("where"))
            {
                sqlToAdd += " and ";
            }
            else
            {
                sqlToAdd += " where ";
            }
            sqlToAdd += parameterSql;
            int pos = sql.IndexOf("order", StringComparison.CurrentCultureIgnoreCase);
            if (pos > 0)
            {
                sql = sql.Substring(0, pos) + sqlToAdd + sql.Substring(pos);
            }
            else
            {
                sql += sqlToAdd;
            }
        }


    }
}
